%{
AUTHOR: Felipe Arteaga
-------------------------------------------------------------------------
PROJECT: Warnings
-------------------------------------------------------------------------
DESCRIPTION:

Genera una base que imita dataRD para el análiss de los
SMSs del 2016. Se base en input creado en "genDatasetsAnalisisSMS.do"

Detalles de las intervenciones en compilado.pdf. Pero el resumen es:


==============
T1: SMSs enviados el viernes 02 de sept, alrededor de las 15:00.
A todos los con menos de 5 postulaciones, en punta arenas o básica de pto natales
(también se mandó mail a un 50%, pero no lo estoy considerando)
* Mensaje:
A: "Postular a la mayor cantidad de colegios posible aumenta las posibilidades de ser admitido en alguno de tu lista."
B: "Una postulacion que incluya entre 5 y 10 colegios aumenta las posibilidades de ser admitido en alguno de tu lista."
C: "Apoderados que han realizado una adecuada postulacion incluyen 5 establecimientos o mas a su lista de preferencias."

{More schools, higher chances:} Tip 1 of the School Admission System: Applying to as many school as possible increases the chances of being admitted to one of your list.
{Range suggestion:} Tip 1 of the School Admission System: An application that includes between 5 and 10 schools increases the chances of being admitted to one of your list.
{Role model:} Tip 1 of the School Admission System: Families who have made a good application include 5 or more schools to their list of preferences.

[Postulación fue entre el día 9 y el 58]

Postulaciones sin efecto de T1: 33 (Inicio de envio T1 en la tarde)
Ultimo efecto sin contamin de T2: 44 (Inicio de envio T2 en la tarde)
Ultimo efecto sin contamin de T5: 53 (Inicio de envio T5: a controles de T2)


==============
T2: SMSs enviados el martes 13 de sept, alrededor de las 18:00.
* Mensaje:
A2: "Tip X del Sistema de Admision Escolar: la postulacion de NOMBRE a CURSO tiene NUMCOLES colegios, te recomendamos postular a 5 o mas para mejorar la postulacion"
B2: "Recuerda que puedes modificar tu postulacion las veces que quieras en la pagina web del sistema de admision escolar, hasta el 23 de septiembre."

Tip 2 of the School Admission System: JUAN's application to PK has 3 schools, we recommend applying to 5 or more to improve the application.

Solo a grupo pta arenas o basica natales y solo a postulantes con ranking de largo 4 o menos
Segundo mensaje (Recuerda que...) se envió a solo la mitad

Postulaciones sin efecto de T2 (pero si T1!): 44 (Inicio de envio T2 en la tarde)
Ultimo efecto sin contamin de T5: 53 (Inicio de envio T5: a controles de T2)
=========================================================================
%}


clearvars -except projectDir projectDirData fromMainWarningsPaper
clc;close all;fclose('all');feature('DefaultCharacterSet','UTF-8');

if(not(exist('projectDir','var')==1&&exist('projectDirData','var')==1&&exist('fromMainWarningsPaper','var')==1&&fromMainWarningsPaper))
    pcName=char(java.lang.System.getProperty('user.name'));
    if(strcmp(pcName,'felipe'))
        % PC Felipe
        myDir='/Users/felipe/Dropbox/';
        projectDir=[myDir,'git/warnings/'];
        projectDirData=[myDir,'projects/warnings/'];
        addpath(genpath([myDir,'/myMatlabFunctions/']));
    end
end
compileLatexTable=false;

dirTable=[projectDir,'/paper/tablesCL/'];
dirData=[projectDirData,'/data/chile/'];

% Load data 2016:
load([dirData,'2016/inputRCT']);

% 
% figure
% tabflow(datosGenerales.tipoSMS_T1,datosGenerales.tipoSMS_T2)
% 
% figure
% binsreg(datosGenerales.riskExpost_33,datosGenerales.riskPredicho_33,'bs','equally-spaced')
% 
% figure
% binsreg(datosGenerales.riskExpost_44,datosGenerales.riskPredicho_44,'bs','equally-spaced')

%% Regs


outcomesLabel={'Add any'};
outcomes={'addAny'};



cantO=length(outcomes);
bT1=nan(cantO,1);
bT1Risky=nan(cantO,1);
bT1ABC=nan(cantO,3);
bT2=nan(cantO,1);
bT2T1=nan(cantO,2);
bT2AB=nan(cantO,2);

NT_bT1=nan(cantO,1);
NT_bT1Risky=nan(cantO,1);
NT_bT1ABC=nan(cantO,3);
NT_bT2=nan(cantO,1);
NT_bT2T1=nan(cantO,2);
NT_bT2AB=nan(cantO,2);

NC_bT1=nan(cantO,1);
NC_bT1Risky=nan(cantO,1);
NC_bT1ABC=nan(cantO,3);
NC_bT2=nan(cantO,1);
NC_bT2T1=nan(cantO,2);
NC_bT2AB=nan(cantO,2);

seT1=nan(cantO,1);
seT1Risky=nan(cantO,1);
seT1ABC=nan(cantO,3);
seT2=nan(cantO,1);
seT2T1=nan(cantO,2);
seT2AB=nan(cantO,2);

datosGenerales.T1=scalarForTable('',datosGenerales);
datosGenerales.T1(datosGenerales.treat_T1)={'T1'};
datosGenerales.T1(datosGenerales.control_T1)={'C1'};
datosGenerales.T1=categorical(datosGenerales.T1);

datosGenerales.T2=scalarForTable('',datosGenerales);
datosGenerales.T2(datosGenerales.treat_T2)={'T2'};
datosGenerales.T2(datosGenerales.control_T2)={'C2'};
datosGenerales.T2=categorical(datosGenerales.T2);

datosGenerales.multiT=categorical(cellfun(@(x,y)[x,'-',y],string(datosGenerales.T1),string(datosGenerales.T2),'UniformOutput',false));

for o=1:cantO
    outcome=outcomes{o};
% T1
tI=33;
tF=44;
tE=59;
datosPostulacion.pobT1=ismember(datosPostulacion.idPostulante,datosGenerales.idPostulante(datosGenerales.pobT1));
postI=stataCollapse({'idPostulante'},datosPostulacion(datosPostulacion.dia==tI&datosPostulacion.pobT1,:),'rbd','set','count',true);
postF=stataCollapse({'idPostulante'},datosPostulacion(datosPostulacion.dia==tF&datosPostulacion.pobT1,:),'rbd','set','count',true);
postE=stataCollapse({'idPostulante'},datosPostulacion(datosPostulacion.dia==tE&datosPostulacion.pobT1,:),'rbd','set','count',true);
assert(all(postI.idPostulante==postF.idPostulante))
assert(all(postE.idPostulante==postF.idPostulante))

datos=table;
datos.idPostulante=postI.idPostulante;
datos.addAny=cellfun(@(x,y)any(not(ismember(x,y))),postF.rbd_set,postI.rbd_set);
datos.addAnyPreSEnd=cellfun(@(x,y)any(not(ismember(x,y))),postE.rbd_set,postI.rbd_set);

datos.schoolsAdded=cellfun(@(x,y)length(x)-length(y),postF.rbd_set,postI.rbd_set);


datos=outerjoin(datosGenerales,datos,'keys',{'idPostulante'},'mergeKeys',true,'type','left');
subpop=datos.pobT1&datos.grupo_T1=='1'&datos.cantprefporpost_T1<5;

% warning('CHANGING SUBPOP')
% % Saco todo el T2, pq T5 trata a los controles de T2.
% % Saco a los riksy en 33, pq si no estoy seleccionando riesgoss que no
% % reaccionaron.
% subpop=datos.pobT1&datos.grupo_T1=='1'&datos.cantprefporpost_T1<5&not(datos.pobT2==1)&datos.riskPredicho_33<0.01;



% First stage
fs=mean(datos.recibioAlguno_T1(subpop&datos.tipoSMS_T1~='Control'))-mean(datos.recibioAlguno_T1(subpop&datos.tipoSMS_T1=='Control'));

reg=table;
reg.T1=double(datos.treat_T1);
reg.out=double(datos.(outcome));
res=fitlm(reg(subpop,:));
[~,se_r,beta]=hac(res,'type','HC','weight','HC1','smallT',false,'display','full'); % HC1 equiv a vce(rob), HC2 a vce("hc2"), HC3 a vce("hc3") [ALL with "'smallT',false" !]
bT1(o)=beta(2);
seT1(o)=se_r(2);
NT_bT1(o)=sum(reg.T1(subpop));
NC_bT1(o)=sum(not(reg.T1(subpop)));


reg=table;
reg.T1=double(datos.treat_T1);
reg.out=double(datos.(outcome));
res=fitlm(reg(subpop&datos.riskPredicho_33>0.01,:));
[~,se_r,beta]=hac(res,'type','HC','weight','HC1','smallT',false,'display','full'); % HC1 equiv a vce(rob), HC2 a vce("hc2"), HC3 a vce("hc3") [ALL with "'smallT',false" !]
bT1Risky(o)=beta(2);
seT1Risky(o)=se_r(2);
NT_bT1Risky(o)=sum(reg.T1(subpop&datos.riskPredicho_33>0.01));
NC_bT1Risky(o)=sum(not(reg.T1(subpop&datos.riskPredicho_33>0.01)));


reg=table;
reg.A=double(datos.tipoSMS_T1=='SMS A');
reg.B=double(datos.tipoSMS_T1=='SMS B');
reg.C=double(datos.tipoSMS_T1=='SMS C');
reg.out=double(datos.(outcome));
res=fitlm(reg(subpop,:));
[~,se_r,beta]=hac(res,'type','HC','weight','HC1','smallT',false,'display','full'); % HC1 equiv a vce(rob), HC2 a vce("hc2"), HC3 a vce("hc3") [ALL with "'smallT',false" !]
bT1ABC(o,:)=beta(2:end);
seT1ABC(o,:)=se_r(2:end);
NT_bT1ABC(o,1)=sum(reg.A(subpop));
NT_bT1ABC(o,2)=sum(reg.B(subpop));
NT_bT1ABC(o,3)=sum(reg.C(subpop));
NC_bT1ABC(o,:)=sum(not(reg.A(subpop))&not(reg.B(subpop))&not(reg.C(subpop)));



figure
barmean(datos(subpop,:),'tipoSMS_T1',outcome,'minPValForPlottingDifference',0.1,'all',0)

figure
barmean(datos(datos.riskExpost_33>0.3&subpop,:),'multiT','addAny')
title('Riskies addAny clean T2')

%% Regs

% T2
tI=44;
tF=53;
tE=59;
datosPostulacion.pobT2=ismember(datosPostulacion.idPostulante,datosGenerales.idPostulante(datosGenerales.pobT2));
postI=stataCollapse({'idPostulante'},datosPostulacion(datosPostulacion.dia==tI&datosPostulacion.pobT2,:),'rbd','set','count',true);
postF=stataCollapse({'idPostulante'},datosPostulacion(datosPostulacion.dia==tF&datosPostulacion.pobT2,:),'rbd','set','count',true);
postE=stataCollapse({'idPostulante'},datosPostulacion(datosPostulacion.dia==tE&datosPostulacion.pobT2,:),'rbd','set','count',true);

assert(all(postI.idPostulante==postF.idPostulante))
assert(all(postI.idPostulante==postE.idPostulante))


datos=table;
datos.idPostulante=postI.idPostulante;
datos.addAny=cellfun(@(x,y)any(not(ismember(x,y))),postF.rbd_set,postI.rbd_set);
datos.addAnyPreSEnd=cellfun(@(x,y)any(not(ismember(x,y))),postE.rbd_set,postI.rbd_set);
datos.schoolsAdded=cellfun(@(x,y)length(x)-length(y),postF.rbd_set,postI.rbd_set);


datos=outerjoin(datosGenerales,datos,'keys',{'idPostulante'},'mergeKeys',true,'type','left');
subpop=datos.pobT2&datos.riskPredicho_44>0.01;% Ya incluye Grupo==1 (pta arenas + basica natales) y menos de 5 preferencias


% First stage
fs=mean(datos.recibioAlguno_T2(subpop&datos.tipoSMS_T2~='Control'))-mean(datos.recibioAlguno_T2(subpop&datos.tipoSMS_T2=='Control'));

reg=table;
reg.T2=double(datos.treat_T2);
reg.out=double(datos.(outcome));
res=fitlm(reg(subpop,:));
[~,se_r,beta]=hac(res,'type','HC','weight','HC1','smallT',false,'display','full'); % HC1 equiv a vce(rob), HC2 a vce("hc2"), HC3 a vce("hc3") [ALL with "'smallT',false" !]
bT2(o)=beta(2:end);
seT2(o)=se_r(2:end);
NT_bT2(o)=sum(reg.T2(subpop));
NC_bT2(o)=sum(not(reg.T2(subpop)));


reg=table;
reg.A2only=double(datos.tipoSMS_T2=='SMS A2');
reg.A2B2=double(datos.tipoSMS_T2=='SMS A2 + SMS B2');
reg.out=double(datos.(outcome));
res=fitlm(reg(subpop,:));
[~,se_r,beta]=hac(res,'type','HC','weight','HC1','smallT',false,'display','full'); % HC1 equiv a vce(rob), HC2 a vce("hc2"), HC3 a vce("hc3") [ALL with "'smallT',false" !]
bT2AB(o,:)=beta(2:end);
seT2AB(o,:)=se_r(2:end);
NT_bT2AB(o,1)=sum(reg.A2only(subpop));
NT_bT2AB(o,2)=sum(reg.A2B2(subpop));
NC_bT2AB(o,:)=sum(not(reg.A2only(subpop))&not(reg.A2B2(subpop)));

reg=table;
reg.treatT2_sinT1=double(datos.treat_T2&not(datos.treat_T1));
reg.treatT2_conT1=double(datos.treat_T2&(datos.treat_T1));
reg.out=double(datos.(outcome));
res=fitlm(reg(subpop,:));
[~,se_r,beta]=hac(res,'type','HC','weight','HC1','smallT',false,'display','full'); % HC1 equiv a vce(rob), HC2 a vce("hc2"), HC3 a vce("hc3") [ALL with "'smallT',false" !]
bT2T1(o,:)=beta(2:end);
seT2T1(o,:)=se_r(2:end);
NT_bT2T1(o,1)=sum(reg.treatT2_sinT1(subpop));
NT_bT2T1(o,2)=sum(reg.treatT2_conT1(subpop));
NC_bT2T1(o,:)=sum(not(reg.treatT2_sinT1(subpop))&not(reg.treatT2_conT1(subpop)));


figure
barmean(datos(subpop,:),'tipoSMS_T2',outcome,'minPValForPlottingDifference',0.1,'all',0)

figure
barmean(datos(datos.riskExpost_44>0.3&subpop,:),'multiT','addAny')
title('Riskies addAny clean T2')

end
%% Table
opt=struct;
opt.header={'T1','T1','T1','T1','T1','T2','T2','T2','T2','T2';...
    'All','Risk>0.01','SMS A','SMS B','SMS C','All','wo/B2','w/B2','wo/T1','w/T1'};
opt.columnasfantasma=5;
matTable=[bT1,bT1Risky,bT1ABC,bT2,bT2T1,bT2AB];
opt.columnaFantasma=[1 2 5 6 8];
opt.firstColumn=outcomes';
opt.adjust=true;
opt.addnumber=true;
opt.positionParameter='H';
opt.stderrs=mat2cellstr([seT1,seT1Risky,seT1ABC,seT2,seT2T1,seT2AB],'conParentesis',true,'precisionDecimal','%.3f');
opt.note='Estimates are differences in mean with respect to a control group that did not get any message. T1 and T2 outcomes are measured after 11 and 9 days after the SMSs were sent';
opt.title='RCT Estimates of SMSs Campaigns T1 and T2 -- 2016';
a=cell2latex(mat2cellstr(matTable,'precisionDecimal','%.3f','revisarFilas',true),'opts',opt);
if(compileLatexTable)
compileLatex(a);
end
%
%% Table 2
opt=struct;
% opt.header={'General SMS','General SMS','General  SMS - By message type','General  SMS - By message type','General  SMS - By message type','Personalized  SMS';...
%     'Pooled','Risk>0.01','More schools,','Range','Role','';...
%     '','','higher chances','suggestion','model',''};

opt.header={'Pooled','Risk>0.01','By message type','By message type','By message type';...
    '','','More schools,','Range','Role';...
    '','','higher chances','suggestion','model'};
opt.columnasfantasma=5;
matTable=[bT1,bT1Risky,bT1ABC;...,bT2;...
    NT_bT1,NT_bT1Risky,NT_bT1ABC;...,NT_bT2;...
    NC_bT1,NC_bT1Risky,NC_bT1ABC];%,NC_bT2];
opt.columnaFantasma=[1 2];
opt.firstColumn=[outcomesLabel';'N Treatment';'N Control'];
opt.filaFantasma=cantO;
opt.adjust=true;
opt.addnumber=true;
opt.stderrs=mat2cellstr([seT1,seT1Risky,seT1ABC;nan(2,5)],'conParentesis',true,'precisionDecimal','%.3f');
opt.positionParameter='H';
opt.note='RCT effect estimates for behavioral nudge interventions conducted as part of the 2016 choice process. These interventions encouraged people to add schools to their lists but did not include information on nonplacement risk. The sample is limited to the Puntarenas region, which was the only region with centralized choice in 2016.  Estimates are differences in the share of students adding any school to their baseline application between the treatment group and a control group that did not get any message. Columns 1 and 2 are pooled estimates of the treatments from columns 3-5. Column 2 limits the sample to applicants facing non-zero application risk. See section \ref{sec:2016Nudges} and Online Appendix \ref{app:pol-int} for details.'; 
opt.file=[dirTable,'2016_tableArmsRCT'];
opt.label='tab:2016RCTarms';
opt.title='RCT Estimates of Behavioral Nudge Effects';
a=cell2latex(mat2cellstr(matTable,'precisionDecimal','%.3f','revisarFilas',true),'opts',opt);
if(compileLatexTable)
compileLatex(a)
end


